/**
 * 
 * 新增时间：2010-12-24
 * 新增人：朱家进
 * 新增内容：验证上传文件内容合法性
 */

package com.zjht.gmcc.servlet.sys;

import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;
import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MySession;

public class OrgaBatchUploadCheckFD extends BaseServlet {

	private static final long serialVersionUID = 1L;

	public boolean process(HttpServletRequest request, HttpServletResponse response,HttpSession session, PrintWriter out, MyJDBC myJDBC,  String menu)throws Exception {

		String dataList = request.getParameter("dataList");// 上传文件内容
		String[] line = dataList.split(",");
		String organame = null;
		String orgacode = null;
		String mobileorgacode = null;
		String countryorgacode = null;
		String halltime = null;
		String halltype = null;
		String hour = null;
		String money = null;
		String[] linedes = null;
		//时间格式 00:00-23:59
		String regxHour = "(20|21|22|23|[0-1]{1}\\d):[0-5]?\\d\\-(20|21|22|23|[0-1]{1}\\d):[0-5]?\\d";
		String regxMoney = "^\\+?[1-9][0-9]*$";

		List<String> errorMsgList = new ArrayList<String>();
		List<String> errorLineList = new ArrayList<String>();
		List<String> trueLineList = new ArrayList<String>();
		
		String isOrgacodeSql = "SELECT * FROM organiza WHERE orgacode = ?";
		PreparedStatement isOrgacodePs = myJDBC.prepareStatement(isOrgacodeSql);
		ResultSet isOrgacodeRs = null;
		String isMobileOrgacodeSql = "SELECT * FROM mobileorgatolocal WHERE mobileorgacode = ? ";
		PreparedStatement isMobileOrgacodePs = myJDBC.prepareStatement(isMobileOrgacodeSql);
		ResultSet isMobileOrgacodeRs = null;
		String isQxSql = "SELECT * FROM organiza WHERE orgacode = ? AND areaid = ?";
		PreparedStatement isQxPs = myJDBC.prepareStatement(isQxSql);
		ResultSet isQxRs = null;
	
		String areaid = String.valueOf(MySession.getAreaid(session));
		String areaShortName = null;
		
		String areaidSql = "SELECT shortname FROM area WHERE areaid = ?";
        PreparedStatement areaidPs = myJDBC.prepareStatement(areaidSql);
        areaidPs.setString(1,areaid);
        ResultSet areaidRs = areaidPs.executeQuery();       
        if(areaidRs.next()){
        	areaShortName = areaidRs.getString("shortname");
        }
        
		Pattern pHour = Pattern.compile(regxHour); 
		Pattern pMoney = Pattern.compile(regxMoney); 
		
		for(int i = 0; i< line.length ; i++){
			linedes = line[i].split("\\|");
			organame = linedes[0];
			orgacode = linedes[1].toUpperCase();
			mobileorgacode = linedes[2].toUpperCase();
			countryorgacode = linedes[3].toUpperCase();
			halltime = linedes[4];
			halltype = linedes[5];
			hour = linedes[6];
			money = linedes[7];
			
			isOrgacodePs.setString(1,areaShortName.toUpperCase()+countryorgacode+"FD"+orgacode);
			isOrgacodeRs = isOrgacodePs.executeQuery();
			if(isOrgacodeRs.next())
			{
				errorMsgList.add("网点名称 <font color='red'>"+organame+"</font> 的网点ZSMS组织编码 <font color='red'>"+orgacode+"</font> 在系统中已存在,请重新确认");
				errorLineList.add(line[i].toString());
				continue;
			}
			isMobileOrgacodePs.setString(1,mobileorgacode);
			isMobileOrgacodeRs = isMobileOrgacodePs.executeQuery();
			if(isMobileOrgacodeRs.next())
			{
				errorMsgList.add("网点名称 <font color='red'>"+organame+"</font> 的BOSS渠道标识 <font color='red'>"+mobileorgacode+"</font> 在系统中已存在,请重新确认");
				errorLineList.add(line[i].toString());
				continue;
			}
			isQxPs.setString(1,countryorgacode);
			isQxPs.setString(2,areaid);
			isQxRs = isQxPs.executeQuery();
			if(!isQxRs.next())
			{
				errorMsgList.add("网点名称 <font color='red'>"+organame+"</font> 的网点所属区/县ZSMS编码 <font color='red'>"+countryorgacode+"</font> 在系统中不存在,请重新确认");
				errorLineList.add(line[i].toString());
				continue;
			}
			if(MyConstant.SYS_ORGA_HALLTIME.indexOf(","+halltime+",") == -1){
				errorMsgList.add("网点名称 <font color='red'>"+organame+"</font> 的网点配送时限 <font color='red'>"+halltime+"</font> 在系统中不存在,请重新确认");
				errorLineList.add(line[i].toString());
				continue;				
			}
			if(MyConstant.SYS_ORGA_HALLTYPE.indexOf(","+halltype+",") == -1){
				errorMsgList.add("网点名称 <font color='red'>"+organame+"</font> 的网点服务厅类别 <font color='red'>"+halltype+"</font> 在系统中不存在,请重新确认");
				errorLineList.add(line[i].toString());
				continue;				
			}			
			  
		    Matcher mHour = pHour.matcher(hour);
		    mHour.matches();
			if(!mHour.matches()){
				errorMsgList.add("网点名称 <font color='red'>"+organame+"</font> 的网点营业时间 <font color='red'>"+hour+"</font> 格式不符,请重新确认");
				errorLineList.add(line[i].toString());
				continue;				
			}
			
		    Matcher mMoney = pMoney.matcher(money);
			if(!mMoney.matches()){
				errorMsgList.add("网点名称 <font color='red'>"+organame+"</font> 的网点最高限额 <font color='red'>"+money+"</font> 格式不符,请重新确认");
				errorLineList.add(line[i].toString());
				continue;				
			}
			
			trueLineList.add(line[i].toString());
			
		}
		
		session.setAttribute("errorMsgList", errorMsgList);
		session.setAttribute("errorLineList", errorLineList);
		session.setAttribute("trueLineList", trueLineList);		
		
		redirect(request,response,"/sys/orga/orgabatch_fdhall_view.jsp");
		
		return 0 > 0;

	}
}
